layui.use(['element','form','layer','jquery','laydate'],function(){
    var form = layui.form,
    	element = layui.element,
		layer = layui.layer,
        $ = layui.jquery,
        laydate=layui.laydate;

    //加载页面日期控件
    laydate.render({
    	elem: '#month'
		,format: 'yyyy-MM' //可任意组合
		,type: 'month'
	    ,ready: function (date) {
	        $(".layui-laydate").on('click', 'ul li', function () {
	            $(".layui-laydate").remove();
	        });
	    }
	    ,change: function (value) {
	        $("#month").val(value);
	    }
	});

    $("#count").click(function(){
    	if($("#month").val() === ""){
    		layer.msg("请先设定计算月份！");
    		return;
    	}
    	if($("#month").val() === getNowFormatDate().substring(0,7)){
    		layer.msg("只能计算本月之前的数据！");
    		return;
    	}
        $("#msg").html("");
    	//弹出loading
    	var indx;
    	$.ajax({
			url : "/manage/cost_count/count_before",
			type : "post",
			data:$("#month").val(),
			dataType : "json",
			async:true,
			contentType:"application/json",
			beforeSend:function(){
				indx = top.layer.msg('计算前检查计算月数据，请稍后！请勿做其他操作',{icon: 16,time:false,shade:0.8});
			},
			success : function(d_before){
				top.layer.close(indx);
				if(d_before.code == '200'){
					var idx;
					// setInterval(function(){
					// 	$.post('/costCount/requestProcess',function(d){
					// 		element.progress('demo', (d-1<0?0:d-1)+'%');
					// 	},'json');
					// }, 2000);
					$.ajax({
						url: "/manage/cost_count/count",
						type: "post",
						data: $("#month").val(),
						dataType: "json",
						async: true,
						contentType: "application/json",
						beforeSend: function () {
							idx = top.layer.msg('计算中，请稍后！请勿做其他操作', {icon: 16, time: false, shade: 0.1});
							// $("#countProcess").removeClass("layui-hide");
						},
						success: function (d) {
							top.layer.close(idx);
							if(d.code == '200'){
								layer.alert('计算完成');
							}else{
								layer.alert('计算失败，计算过程中发生错误');
							}
							$("#msg").html(d.msg);
						}
						,error: function () {
							top.layer.close(idx);
							alert("计算失败！");
						}
					})
				}else{
					top.layer.msg("检查未通过，请根据提示完善或修改数据！");
					$("#msg").html(d_before.msg);
					return false;
				}
			}
			,error: function () {
				top.layer.close(indx);
				alert("计算失败！");
			}
    	});
    })

    function countTime(){
    	$.post('/costCount/requestProcess',function(d){
    		console.log(d);
    		element.progress('demo', d+'%');
    	},'json');
    }
    function count(){
    	var idx;

    }
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
})